package com.atguigu.web01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-08-24 10:04 星期四
 * description:
 */
@Controller   //不需要返回JSON数据
public class RequestController {
    
    @GetMapping("goto")
    public String gotoPage(HttpServletRequest request){
        request.setAttribute("message","成功了");
        request.setAttribute("code",200);
        return "forward:/success";   //转发到   /success请求
    }
    
    @GetMapping("/params")
    public String testParam(Map<String,Object> map,
                            Model model,
                            HttpServletRequest request,
                            HttpServletResponse response){
        map.put("hello","world666");
        model.addAttribute("world","hello666");
        request.setAttribute("message2","hello world");
        Cookie cookie = new Cookie("c1","v1");
//        cookie.setDomain("localhost");   //设置cookie的作用域
        response.addCookie(cookie);
        return "forward:/success";
        
    }


    @ResponseBody
    @GetMapping("/success")
    public Map<String,Object> success(@RequestAttribute(value = "message",required = false) String msg,
                                      HttpServletRequest request){
        Object code = request.getAttribute("code");
        Map<String,Object> map = new HashMap<>();
//        map.put("message",msg);
//        map.put("code",code);
        
        //   ---------------------
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message2 = request.getAttribute("message2");

        map.put("hello",hello);
        map.put("world",world);
        map.put("message2",message2);
        
        return map;
    }
    
}
